class Solution(object):
    def circularArrayLoop(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        n = len(nums)
        used = [-1] * n
        flag = 0
        for i in range(n):
            if used[i] < 0:
                index = i
                while used[index] < 0:
                    used[index] = flag
                    index = (index + nums[index]) % n
                if used[index] == flag:
                    count = 0
                    step = 0
                    first = index
                    while True:
                        next_index = (index + nums[index]) % n
                        count += 1
                        if nums[index] > 0:
                            step += 1
                        else:
                            step -= 1
                        if next_index == first:
                            break
                        else:
                            index = next_index
                    if count > 1 and count == abs(step):
                        return True
                flag += 1
        return False


data = Solution()
nums = [2, -1, 1, 2, 2]
print(data.circularArrayLoop(nums))
nums = [-1, 2]
print(data.circularArrayLoop(nums))
nums = [-2, 1, -1, -2, -2]
print(data.circularArrayLoop(nums))
